package app.transmission.remote.activity;

import com.google.inject.Inject;

import roboguice.activity.RoboActivity;

import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import app.transmission.remote.R;
import app.transmission.remote.common.MethodCotexts;
import app.transmission.remote.common.ParamCotexts;
import app.transmission.remote.connect.DaemonConnect;
import app.transmission.remote.json.domain.Torrent;
import app.transmission.remote.json.request.JsonRequest;
import app.transmission.remote.json.response.TorrentResponse;

public class MainActivity extends RoboActivity {
	
	@Inject
	private DaemonConnect daemonConnect;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		JsonRequest request = new JsonRequest();
		request.setMethod(MethodCotexts.TORRENT_GET);
		request.setParam("fields", ParamCotexts.TORRENT_FIELDS);
		TorrentResponse response = daemonConnect.postResponse(request, TorrentResponse.class);
		LinearLayout layout = (LinearLayout) findViewById(R.id.main_body);
		Torrent[] torrents = response.getArguments().getTorrents();
		for (Torrent torrent : torrents) {
			View child = getLayoutInflater().inflate(R.layout.torrent_body, null);
			TextView name = (TextView) child.findViewById(R.id.torrent_name);
			name.setText(torrent.getName());
			layout.addView(child);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
